'use strict';

const Service = require('egg').Service;

class UpdateService extends Service {
  /**
   *删除
   *
   * @param {*} obj
   * @returns
   * @memberof UpdateService
   */
  async upUser(obj) {
    const { ctx } = this;
    return new Promise( async (resolve, reject) => {
        let findOne = await ctx.model.User.findOne(obj);
        
        if(!findOne) return resolve({ code: 201, msg: '密码错误！'});
        let upDateOne = await ctx.model.User.updateOne(obj, {role: -1});
        // console.log(upDateOne);
        if (!upDateOne) return resolve({ code: 201, msg: '删除失败！'});
        return resolve({ code: 200, msg: '用户删除成功！' });
    })
  }
  /**
   *删除
   *
   * @param {*} obj
   * @returns
   * @memberof UpdateService
   */
  async delOrder(obj) {
    const { ctx } = this;
    return new Promise( async (resolve, reject) => {
        let upDateOne = await ctx.model.Order.updateOne({ _id: obj._id }, { state: obj.state });
        if (!upDateOne) return resolve({ code: 201, msg: '项目跟新失败！'});
        return resolve({ code: 200, msg: '项目跟新成功！' });
    })
  }
  
  /**
   *跟新消费项目
   *
   * @param {*} obj
   * @returns
   * @memberof UpdateService
   */
  async upOrder(obj) {
    const { ctx } = this;
    obj.price = JSON.stringify(obj.price);
    return new Promise( async (resolve, reject) => {
        let upDateOne = await ctx.model.Order.updateOne({ _id: obj._id}, obj);
        // console.log(upDateOne);
        if (!upDateOne) return resolve({ code: 201, msg: '跟新失败！'});
        return resolve({ code: 200, msg: '更新规格成功！' });
    })
  }
}

module.exports = UpdateService;
